﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.BusinessLogic.Common
{
    /// <summary>
    /// Class used as a (guid based enumeration) list that holds the possible types for Country.
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.CountryFactory, MarketPulse.DataAccess.Factory", "FetchInfoList")]
    public class CountryInfoList : MarketPulseReadOnlyListBase<CountryInfoList, CountryInfo>
    {
        #region Member Variables

            /// <summary>List to hold all the possible Country values</summary>
            private static CountryInfoList mCountries;

        #endregion

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// via only the static factory methods provided.
            /// </summary>
            private CountryInfoList()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method used to retrieve a list of Country objects. Internally this accesses a static field so this
                /// class is effectively a lazily instantiated singleton.
                /// </summary>
                /// <param name="profileTypeGUID">The profile type to search for</param>
                /// <returns>A valid (populated) list of Country objects</returns>
                public static CountryInfoList Get(Guid profileTypeGUID)
                {
                    // Load the list if it's null
                    if (mCountries == null)
                    {
                        mCountries = DataPortal.Fetch<CountryInfoList>(new SingleCriteria<Guid>(profileTypeGUID));
                    }
                    return mCountries;
                }

                /// <summary>
                /// Method used to retrieve a list of Country objects. Internally this accesses a static field so this
                /// class is effectively a lazily instantiated singleton.
                /// </summary>
                /// <returns>A valid (populated) list of Country objects</returns>
                public static CountryInfoList GetAll()
                {
                    // Load the list if it's null
                    if (mCountries == null)
                    {
                        mCountries = DataPortal.Fetch<CountryInfoList>();
                    }
                    return mCountries;
                }

            #endregion

                /// <summary>
                /// Method used to find a single CountryInfo object from the collection based on the unique identifier supplied
                /// </summary>
                /// <param name="countryGuid">The guid that uniquely identifies the Country Info object to find</param>
                /// <returns>A populated Country Info object</returns>
                public CountryInfo FindByGuid(Guid countryGuid)
                {
                    var result = (from country in this.Items
                                  where country.GUID == countryGuid
                                  select country).Single();

                    return (CountryInfo)result;
                }

        #endregion

    }
}
